{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f=open(\"file.txt\",mode='r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.write('i love study')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'i love pythoni love study'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-5\n"
     ]
    }
   ],
   "source": [
    "#split('',num)   将一个字符串分开\n",
    "import random\n",
    "from random import randint\n",
    "x=randint(-10,10)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/shihao-z'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#OS  操作系统\n",
    "import os\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.local',\n",
       " 'Untitled.ipynb',\n",
       " '.ipynb_checkpoints',\n",
       " 'file.txt',\n",
       " '.PyCharm2017.3',\n",
       " '下载',\n",
       " '图片',\n",
       " '.gconf',\n",
       " 'cmake-build-debug',\n",
       " '.mozilla',\n",
       " 'clion-2017.3',\n",
       " '.compiz',\n",
       " '.jupyter',\n",
       " '.ICEauthority',\n",
       " '模板',\n",
       " '公共的',\n",
       " '.conda',\n",
       " '.wiznote',\n",
       " '.xsession-errors',\n",
       " '.bashrc',\n",
       " '.viminfo',\n",
       " 'pycharm-2017.3.1',\n",
       " '.idea',\n",
       " '.pki',\n",
       " '.config',\n",
       " '.xsession-errors.old',\n",
       " '.sudo_as_admin_successful',\n",
       " '.dbus',\n",
       " '视频',\n",
       " '.bash_logout',\n",
       " '.java',\n",
       " '.bashrc-anaconda3.bak',\n",
       " '.CLion2017.3',\n",
       " '.python_history-23230.tmp',\n",
       " '.swp',\n",
       " '2017.12.21 basic.ipynb',\n",
       " '.adobe',\n",
       " '.xinputrc',\n",
       " '.text.c.swp',\n",
       " '音乐',\n",
       " '.gnupg',\n",
       " 'CLionProjects',\n",
       " '.ipython',\n",
       " '.bash_history',\n",
       " '.cache',\n",
       " '.Xauthority',\n",
       " 'anaconda3',\n",
       " '桌面',\n",
       " '.dmrc',\n",
       " 'PycharmProjects',\n",
       " '.macromedia',\n",
       " '.presage',\n",
       " 'examples.desktop',\n",
       " '2017.12.20  basic.ipynb',\n",
       " '.profile',\n",
       " '文档',\n",
       " '.python_history']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.listdir(path='.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['shihao-z']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.listdir(path='..')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "os.mkdir('file2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.local',\n",
       " 'Untitled.ipynb',\n",
       " '.ipynb_checkpoints',\n",
       " 'file.txt',\n",
       " '.PyCharm2017.3',\n",
       " '下载',\n",
       " '图片',\n",
       " '.gconf',\n",
       " 'cmake-build-debug',\n",
       " '.mozilla',\n",
       " 'clion-2017.3',\n",
       " '.compiz',\n",
       " '.jupyter',\n",
       " '.ICEauthority',\n",
       " '模板',\n",
       " '公共的',\n",
       " '.conda',\n",
       " '.wiznote',\n",
       " 'file2',\n",
       " '.xsession-errors',\n",
       " '.bashrc',\n",
       " '.viminfo',\n",
       " 'pycharm-2017.3.1',\n",
       " '.idea',\n",
       " '.pki',\n",
       " '.config',\n",
       " '.xsession-errors.old',\n",
       " '.sudo_as_admin_successful',\n",
       " '.dbus',\n",
       " '视频',\n",
       " '.bash_logout',\n",
       " '.java',\n",
       " '.bashrc-anaconda3.bak',\n",
       " '.CLion2017.3',\n",
       " '.python_history-23230.tmp',\n",
       " '.swp',\n",
       " '2017.12.21 basic.ipynb',\n",
       " '.adobe',\n",
       " '.xinputrc',\n",
       " '.text.c.swp',\n",
       " '音乐',\n",
       " '.gnupg',\n",
       " 'CLionProjects',\n",
       " '.ipython',\n",
       " '.bash_history',\n",
       " '.cache',\n",
       " '.Xauthority',\n",
       " 'anaconda3',\n",
       " '桌面',\n",
       " '.dmrc',\n",
       " 'PycharmProjects',\n",
       " '.macromedia',\n",
       " '.presage',\n",
       " 'examples.desktop',\n",
       " '2017.12.20  basic.ipynb',\n",
       " '.profile',\n",
       " '文档',\n",
       " '.python_history']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.listdir(path='.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "os.rmdir('file2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__call__',\n",
       " '__class__',\n",
       " '__delattr__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__le__',\n",
       " '__lt__',\n",
       " '__module__',\n",
       " '__name__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__qualname__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__self__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " '__text_signature__']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32512"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.system('calc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "error:[Errno 2] No such file or directory: 'jaoda.txt'\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    a=open('jaoda.txt')\n",
    "except OSError as reason:\n",
    "    print('error:'+str(reason))\n",
    "#finally:  总要执行的命令\n",
    " #   a.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10:\n",
      "11:\n",
      "it is sushu\n",
      "12:\n",
      "13:\n",
      "it is sushu\n",
      "14:\n",
      "15:\n",
      "16:\n",
      "17:\n",
      "it is sushu\n",
      "18:\n",
      "19:\n",
      "it is sushu\n",
      "20:\n",
      "21:\n",
      "22:\n",
      "23:\n",
      "it is sushu\n",
      "24:\n",
      "25:\n",
      "26:\n",
      "27:\n",
      "28:\n",
      "29:\n",
      "it is sushu\n"
     ]
    }
   ],
   "source": [
    "def su(N):\n",
    "    \"\"\"求所输入的数字是否是素数\"\"\"\n",
    "    for i in range(2,N):\n",
    "        if N%i==0:\n",
    "            break;\n",
    "    else:\n",
    "        print(\"it is sushu\")\n",
    "for i in range(10,30):\n",
    "    print(\"%d:\"%i)\n",
    "    su(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#else 与if--else   与循环用  while--else  for--else  与try--except--else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i love pythoni love study\n"
     ]
    }
   ],
   "source": [
    "#with\n",
    "with open('file.txt',mode='r') as f:\n",
    "    print(f.readline())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'easygui'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-25-61604b5757b0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m#图形用户界面\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0measygui\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0measygui\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mmsgbox\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'hello'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'easygui'"
     ]
    }
   ],
   "source": [
    "#图形用户界面\n",
    "import easygui\n",
    "from easygui import *\n",
    "msgbox('hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tkinter\n",
    "from tkinter import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def hello():  \n",
    "    print('hello')  \n",
    "def about():  \n",
    "    label = Label(root, text='王小涛_同學\\n QQ:*********', fg='red', bg='black')  \n",
    "    label.pack(expand=YES, fill=BOTH)  \n",
    "root = Tk()  \n",
    "menubar = Menu(root)  \n",
    "filemenu = Menu(menubar, tearoff=0)  \n",
    "filemenu.add_command(label='打开', command=hello)  \n",
    "filemenu.add_command(label='保存')  \n",
    "filemenu.add_separator()  \n",
    "filemenu.add_command(label='退出', command=root.quit)  \n",
    "menubar.add_cascade(label='文件', menu=filemenu)  \n",
    "helpmenu = Menu(menubar, tearoff=0)  \n",
    "helpmenu.add_command(label='关于作者', command=about)  \n",
    "menubar.add_cascade(label='关于', menu=helpmenu)  \n",
    "root.config(menu=menubar)  \n",
    "root.geometry('200x400')  \n",
    "root.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhenshihao\n",
      "zhenshihao\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "#class  __init__ 初始化   __year 只能从内部访问  或者_student__year\n",
    "class student:\n",
    "    __year=18\n",
    "    def __init__(self,a):\n",
    "        self.name=a\n",
    "    def out(self):\n",
    "        print(self.name)\n",
    "i=student('zhenshihao')\n",
    "i.out()\n",
    "print(i.name)\n",
    "print(i._student__year)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my position:7 9\n"
     ]
    }
   ],
   "source": [
    "#继承   子类的对象名字与父类重复，自动覆盖  __init__特殊 通过 父类.__init__(self)依然可以用父类  或者super().__init__()\n",
    "import random\n",
    "from random import randint\n",
    "class fish:\n",
    "    def __init__(self):\n",
    "        self.x=randint(0,10)\n",
    "        self.y=randint(0,10)\n",
    "    def move(self):\n",
    "        self.x-=1\n",
    "        print(\"my position:%d %d\"%(self.x,self.y))\n",
    "class Goldfish(fish):\n",
    "    pass\n",
    "class har(fish):\n",
    "    pass\n",
    "class shark(fish):\n",
    "    def __init__(self):\n",
    "        fish.__init__(self)#====super().__init__()\n",
    "        self.hungry=True\n",
    "    def eat(self):\n",
    "        if self.hungry==True:\n",
    "            print('i am an eater')\n",
    "            self.hungry=False\n",
    "        else:\n",
    "            print('i am not hungry')\n",
    "fish1=fish()\n",
    "fish1.move()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(fish1.x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my position:4 0\n"
     ]
    }
   ],
   "source": [
    "goldfish=Goldfish()\n",
    "goldfish.move()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my position:1 10\n"
     ]
    }
   ],
   "source": [
    "Shark=shark()\n",
    "Shark.move()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i am not hungry\n"
     ]
    }
   ],
   "source": [
    "Shark.eat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#多重继承   class jieheti(fish,shark):  pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#组合  直接将类放入组合类里实例化\n",
    "class tutle:\n",
    "    def __init__(self,x):\n",
    "        self.num=x\n",
    "    def polite(self):\n",
    "        print(\"hello\")\n",
    "class Fish:\n",
    "    def __init__(self,x):\n",
    "        self.num=x\n",
    "    def polite(self):\n",
    "        print(\"everybody hello\")\n",
    "class pool:\n",
    "    def __init__(self,x,y):\n",
    "        self.tutle1=tutle(x)\n",
    "        self.fish1=Fish(y)\n",
    "    def num(self):\n",
    "        print(\"tutle:%d\\nfish:%d\"%(self.tutle1.num,self.fish1.num))\n",
    "    def polite(self):\n",
    "        self.tutle1.polite()\n",
    "        self.fish1.polite()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "p=pool(10,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tutle:10\n",
      "fish:10\n"
     ]
    }
   ],
   "source": [
    "p.num()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "everybody hello\n"
     ]
    }
   ],
   "source": [
    "p.polite()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Fish.polite=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#绑定 一定要实例化类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "issubclass(fish,shark)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "issubclass(shark,fish)#子 父 ->True  第一个参数是第二个参数的子类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "issubclass(shark,object)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(shark,fish)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(fish,shark)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isinstance(pool.num,pool)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i am a student\n"
     ]
    }
   ],
   "source": [
    "#魔法方法  1:__init__  构建类的初始化操作  返回值一定是None\n",
    "class student:\n",
    "    def __init__(self):\n",
    "        print(\"i am a student\")\n",
    "s=student()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i love py\n",
      "i am a student\n"
     ]
    }
   ],
   "source": [
    "#  2:__new__ 构建类第一个被调用的函数  返回值是一个实力对象\n",
    "class student():\n",
    "    def __init__(self):\n",
    "        print('i am a student')\n",
    "    def __new__(cls):\n",
    "        print('i love py')\n",
    "        return object.__new__(cls)\n",
    "stu=student()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "new\n",
      "<class 'type'>\n",
      "init\n"
     ]
    }
   ],
   "source": [
    "class display(object):\n",
    "  def __init__(self, *args, **kwargs):\n",
    "    print(\"init\")\n",
    "  def __new__(cls, *args, **kwargs):\n",
    "    print(\"new\")\n",
    "    print(type(cls))\n",
    "    return object.__new__(cls, *args, **kwargs)  \n",
    "a=display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#__del__(self)  当对象被销毁时 调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
